import {Component, OnInit, Input} from '@angular/core';
import {ApiService} from "@core/services/http/api.service";
import {GridConfigService} from 'app/core/config/grid-config.service';
import {GridOptions} from '@ag-grid-community/core';
import {ApesMessageService} from "@shared/apes/message";
import {UserAuthService} from "@core/services/user-auth.service";
import { BiService } from '../../../bi.service';

@Component({
  selector: 'not-quick-check',
  templateUrl: './not-quick-check.component.html',
  styleUrls: ['./not-quick-check.component.scss']
})
export class NotQuickCheckComponent implements OnInit {
  /* 按钮相关 */
  buttonBar = [{hide: true, style: 'default', label: '查询', id: 'action_query',loading: false},
    {hide: true, style: 'default', label: '展开查询条件', id: 'action_show'},
    // {hide: false, style: 'default', label: '重置查询条件', id: 'action_reset'}
  ];
  title = '';
  ctx = {model: {}};

  /** Grid 相关 **/
  rowData:any[] = [];
  resultData:any;
  gridOptions:GridOptions;
  pinnedBottomRowData:any[] = [];
  columnDefs:any[] = [];
  hidn: any = "none";
  height: any = window.innerHeight - 200 + 'px';

  // 获取门店
  nxmdList: any[];
  arrNXMD = [];
  ngNXMD: any;

  // 获取人名
  nameList: any[];
  arrYGMD = [];
  ngYGMD: any;

  mdjcTime: any[] = [];
  mdjcTime_ks = null;
  mdjcTime_js = null;

  endDate = null;      // 默认结束时间
  startDate = null;    // 默认开始时间 当天的前7天

  userInfo = this.userAuthService.getUserInfoObj();
  companyId = this.userInfo.userAccount.post.company.id;
  companyData :any;
  companyList: any[];

  constructor(private apiService:ApiService,
              private gridConfigService:GridConfigService,
              private msg: ApesMessageService,
              private userAuthService: UserAuthService,
              private bi: BiService) {
    this.columnDefs = [
      {headerName: "责任人", field: 'YGMC'},
      {headerName: "门店", field: 'MDMC'},
      {headerName: "日期", field: 'XZRQ_SJ'},
      {headerName: "接车数", field: 'JCS'},
      {headerName: "本月累计未检测数量", field: 'YWJCS'},
      {headerName: "检查数量", field: 'ZJCS'},
      {headerName: "门店检测数", field: 'MDZJCS'},
      {headerName: "与门店检测占比", field: 'JCZB'}
    ];

    // 初始化 grid
    this.gridOptions = <GridOptions>{
      toolPanelSuppressSideButtons: true,
      suppressExcelExport: true,
      suppressCsvExport: true,
      showToolPanel: false,
      pagination: true,
      paginationPageSize: 100,
      rowHeight: 35
    };
    this.gridOptions.localeText = gridConfigService.getLocaleText();
    this.companyList = [];
    this.companyList.push(this.companyId);
  }

  ngOnInit() {
    // 默认 请求 当天 数据
    this.endDate = new Date();
    this.startDate = new Date(this.endDate.getTime() - (0 * 24 * 60 * 60 * 1000));
    this.mdjcTime.push(this.startDate);
    this.mdjcTime.push(this.endDate);
    this.bi.getCompany(this.userInfo.userAccount.post.localPermit.id).then(value => { this.companyData = value });
    this.submit();
    this.getNxmdData();
  }

  onEvent(data: any) {

    if (data instanceof MouseEvent) return;
    if (data) {
      if (data.id === 'action_query') {
        this.buttonBar[0].loading = true;
        this.submit();
        this.hidn = "none";
      } else if (data.id === 'action_reset') {
        this.action_reset();
      } else if (data.id === 'action_show') {
        this.action_show();
      }
    }
  }



  action_reset() {
  }

  action_show() {
    if (this.hidn === "none") {
      this.hidn = "flex";
    } else if (this.hidn === "flex") {
      this.hidn = "none";
    }
  }

  submit() {
    // if(this.companyList.length <= 0){
    //   this.companyList.push(this.companyId);
    // }
    this.rowData = [];
    // 传的时间 进行转换
    this.getDate(this.mdjcTime, "mdjcTime_ks", "mdjcTime_js");

    this.getTablesData();

  }

  getDate(time: any[], time_ks: string, time_js: string) {
    // debugger;
    let [first, second] = time;
    if (first) {
      this[time_ks] = first.toISOString().slice(0, 10);
    } else {
      this[time_ks] = null;
    }

    if (second) {
      this[time_js] = second.toISOString().slice(0, 10);
    } else {
      this[time_js] = null;
    }

  }

  getTablesData() {
    let data = {'interfaceMethod': 'ipmMgzzForAppService.getQuickData',
      'pdata': {'STARTDATE': this.mdjcTime_ks, 'ENDDATE': this.mdjcTime_js,
                'MDID': this.ngNXMD, 'YGID': this.ngYGMD, 'companyId': this.companyList}};
    this.apiService.post('report.interface', {'body': JSON.stringify(data)}).subscribe(
      (resultData) => {
        const result = JSON.parse(resultData);
        if (result.success) {
          if (result.obj.length !== 0) {
            let data = JSON.parse(result.obj)
            for (let i = 0, len = data.length; i < len; i++) {
              data[i]['JCZB'] = data[i]['JCZB'] + '%';
            }
            this.rowData = data;
            if( this.rowData.length === 0 ) {
              this.msg.info(`未找到满足条件的数据`);
            }
            this.nameList = data;
            this.arrYGMD = this.nameList;

            this.buttonBar[0].loading = false;
          }
        } else {
          this.msg.info(result.msg);
        }
      });
  }

  // 获取门店名称
  getNxmdData() {
    let data = { 'interfaceMethod': 'ipmBPforAppService.getSQMDXX', 'pdata': { 'companyId': this.companyList } };
    this.apiService.post('report.interface', { 'body': JSON.stringify(data) }).subscribe(
      (resultData) => {
        const result = JSON.parse(resultData);
        if (result.success) {
          if (result.obj.length !== 0) {
            let data = JSON.parse(result.obj)
            this.nxmdList = data;
            this.arrNXMD = this.nxmdList;
          }
        } else {
          this.msg.info(result.msg);
        }
      });
  }

}
